<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Base64 Image Viewer</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 800px;
            text-align: center;
        }
        input[type="text"] {
            width: calc(100% - 22px);
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        img {
            max-width: 100%;
            height: auto;
            border: 1px solid #eee;
            border-radius: 4px;
            margin-top: 10px;
        }
        h2 {
            margin-bottom: 20px;
            color: #333;
        }
        .error-message {
            color: red;
            font-size: 0.9em;
            margin-top: 10px;
        }
        .sample-button {
            margin-top: 10px;
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .sample-button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Base64 Image Viewer</h2>
        <input type="text" id="base64Input" placeholder="Enter Base64 string here...">
        <!-- <button class="sample-button" onclick="loadSample()">Load Sample Image</button> -->
        <img id="imageOutput" src="" alt="Image Preview">
        <div id="errorMessage" class="error-message"></div>
    </div>

    <script>
        function loadSample() {
            const sampleBase64 = "iVBORw0KGgoAAAANSUhEUgAAADIA..."; // Truncated for brevity
            document.getElementById('base64Input').value = "data:image/png;base64," + sampleBase64;
            updateImage(sampleBase64);
        }

        function updateImage(base64String) {
            const errorMessageElement = document.getElementById('errorMessage');
            
            if (base64String) {
                try {
                    // Check if the string is a valid Base64 format
                    const decodedData = atob(base64String);
                    const encodedData = btoa(decodedData);
                    
                    if (encodedData !== base64String) {
                        throw new Error("Invalid Base64 string.");
                    }

                    document.getElementById('imageOutput').src = 'data:image/png;base64,' + base64String;
                    errorMessageElement.textContent = '';
                } catch (error) {
                    document.getElementById('imageOutput').src = '';
                    errorMessageElement.textContent = error.message;
                }
            } else {
                document.getElementById('imageOutput').src = '';
                errorMessageElement.textContent = '';
            }
        }

        document.getElementById('base64Input').addEventListener('input', function() {
            let value = this.value.trim();
            // Remove data:image/png;base64, prefix if present
            if (value.startsWith("data:image/png;base64,")) {
                value = value.substring("data:image/png;base64,".length);
            }
            updateImage(value);
        });

        // Load default sample on page load
        window.onload = function() {
            loadSample();
        };
    </script>
</body>
</html>



